https://leetcode.cn/problems/word-search/description/
class Solution {
public:
    int dx[4]={0,0,-1,1};
    int dy[4]={1,-1,0,0};
    int m,n;
    bool check[7][7];
    bool dfs(vector<vector<char>>& board,int row,int col,string &word,int pos)
    {
        if(pos==word.size())
            return true;
        
        for(int i=0;i<4;i++)
        {       
            int x=row+dx[i],y=col+dy[i];
            if(x>=0&&x<m && y>=0&&y<n &&!check[x][y]&&board[x][y]==word[pos])
            {
                check[x][y]=true;
                if(dfs(board,x,y,word,pos+1))
                    return true;
                //回溯
                check[x][y]=false;
            }
        }
        return false;
    }

    bool exist(vector<vector<char>>& board, string& word) {
        m=board.size(),n=board[0].size();

        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(board[i][j]==word[0])
                {
                    check[i][j]=true;
                    if(dfs(board,i,j,word,1))
                        return true;
                    check[i][j]=false;
                }
            }
        }
        return false;
    }
};